require 'countries'
require 'fuel_prices_europe/table_parser'

module FuelPricesEurope

  class Scraper

    def self.scrape

      if FuelPricesEurope.configuration.cache.nil?
        make_request!
        parse
      else
        FuelPricesEurope.configuration.cache.fetch(FuelPricesEurope.configuration.cache_key.to_s,
                                                             expires_in: FuelPricesEurope.configuration.cache_expires_in) do
          make_request!
          parse
        end
      end
    end

    private

    def self.parse
      element = 'table:nth-child(18)'
      table_parser = TableParser.new(@html.at_css(element))
      table_parser.parse
    end

    def self.make_request!
      response = open(URL)
      @html = Nokogiri::HTML(response)
    end

  end
end